import {
    City
} from './city.model'
const city = new City()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        cityList: [],
        cityName: '',
        telephone: '0451-83127808',
        cityModel: false,
        openCities: [{
            code: '0451',
            name: '哈尔滨'
        }]
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad() {
        let that = this,
            userLocation = city.getStorage('userLocation')
        that.setData({
            cityName: userLocation.city
        })
        that._loadData()
    },

    /**
     * 获取所有数据
     */
    _loadData() {
        let that = this
        that.getCityData()
    },

    /**
     * 获取城市列表
     */
    getCityData() {
        let that = this
        city.getCityData().then((res) => {
            that.setData({
                cityList: res.data
            })
        })
    },

    /**
     * 选择城市
     * @param {*} options
     */
    onChooseCity(options) {
        let that = this,
            code = city.getDataSet(options, 'code'),
            prevPage = city.getPrevPage() // 获取上一页
        if ('0451' != code) {
            that.setData({
                cityModel: true
            })
        } else {
            let cityInfo = {
                latitude: '45.737957',
                longitude: '126.633995',
                district_id: '230109',
                cityName: '哈尔滨市',
                isSelected: true
            }
            city.setStorage('cityInfo', cityInfo)
            //直接调用上一个页面的setData()方法，把数据存到上一个页面中去
            prevPage.setData({
                cityName: cityInfo.cityName,
                latitude: cityInfo.latitude,
                longitude: cityInfo.longitude
            })
            wx.navigateBack({
                delta: 1
            })
        }
    },

    /**
     * 拨打电话
     */
    onTapPhone() {
        let that = this
        wx.makePhoneCall({
            phoneNumber: that.data.telephone,
        })
    },

    /**
     * 关闭申请窗口
     */
    onCloseModel() {
        let that = this
        that.setData({
            cityModel: false
        });
    }
})